
import pygame
from pygame import font
import os
import time
from util import * 
from retrogamelib import button
from retrogamelib.constants import *
from copy import copy

from enemys import *

class GameTxtSprite(pygame.sprite.Sprite):
    	group = None
	def __init__(self, txt, pos, size, antialias = True, color = (255,255,255), alpha_val = 255):
		pygame.sprite.Sprite.__init__(self)

	    	self.gamefont_BG = font.Font(os.path.join('gfx', "ARIALN.ttf"), size+1)
	    	self.gamefont = font.Font(os.path.join('gfx', "ARIALN.ttf"), size)

		self.antialias = antialias
		self.pos =pos
		self.txt =txt
		self.color = color
		self.bgcolor = (0,0,0)
		self.alpha_val = alpha_val

		self.image = self.gamefont_BG.render(self.txt, self.antialias , self.color, self.bgcolor)

		self.image.set_colorkey(self.image.get_at((0, 0)), pygame.RLEACCEL)
		self.image.set_alpha(self.alpha_val)
		self.rect = self.image.get_rect()
		self.rect.midbottom = self.pos

		self.layer = pos[1]

		self.group.add(self, layer = self.layer)

		#self.rush = self.rush_effect()
		#self.x = 0

	def move(self, dx, dy):
		self.rect.x += dx
		self.rect.y += dy
		self.BASEMENT[0] += dx
		self.BASEMENT[1] += dy

	def rush_effect(self):
		for self.x in xrange(256):
		    self.image.set_alpha(self.x*3)
		    self.rect[0] += 1
		    yield None
		#self.kill()
				    	
	def update(self, txt):
		self.image = self.gamefont.render(txt, self.antialias ,self.color, self.bgcolor)

		self.image.set_colorkey(self.image.get_at((0, 0)), pygame.RLEACCEL)
		self.image.set_alpha(self.alpha_val)
		self.rect = self.image.get_rect()
		self.rect.midbottom = self.pos

	    	"""
		if self.x <30:
			self.rush.next()
		else:
		    	self.kill()
		"""	

class GameSprite(pygame.sprite.Sprite):
	#group = None

	def __init__(self, pos, filename, group, screen=None):
		print "create a GameSprite"
		pygame.sprite.Sprite.__init__(self)
		self.sheet = load_image(os.path.join('gfx',filename)) 
		self.STANDING = True
		self.WALKING = False
		self.ATTACKING = False
		self.RUNNING = False
		self.HIT = False
		self.JUMPING = False
		self.JUMPING_MOVE = False
		self.JUMPING_MOVE_SPEED = 0
		self.JUMP_SLASH = False
		self.FACING = 1			#1 facing right , -1 facing left
		self.controllable = False

		self.offsetx = 0
		self.offsety = 0
		self.images = {'STAND':[], 'WALK':[] , 'JUMP':[], 'ATTACK':[], 'HIT':[],} 
		self.standframe = self.attackframe = self.walkframe = self.jumpframe = self.slashframe = 0
		self.STANDING_imgframe = self.WALKING_imgframe = self.JUMPING_imgframe = self.ATTACKING_imgframe = self.HITTING_imgframe = 0

		self.FRAME_COUNTER = 0
		self.CPF = 200
		
		self.BASEMENT = list(pos) #bottomcenter
		self.layer = pos[1]
		self.BODY = None		#every one has a body !
		self.HITZONE = None		#when the sprite attack , gen a hitzone for him , let's kick some ass
		self.maxLife = 20
		self.life = 20
		self.screen = screen
		#self.add(self.game.players)
		#self.group.add(self, layer = self.layer)
		self.group = group
		self.group.add(self, layer = self.layer)

	
	def update(self):
	    	pass

	def on_collision(self, dx , dy):
	    	pass
	
	def die(self):
	    	self.kill()
	
	def update_stand(self):
		#self.update_status('STANDING')
		self.standframe += 1
		self.STAND_imgframe = self.standframe%(len(self.images['STAND']))
		self.image = self.images['STAND'][self.STAND_imgframe]
		
		if self.FACING < 0:
			self.image = pygame.transform.flip(self.image, True, False)

	def update_walk(self):
		#self.update_status('WALKING')
		imgframe = 0
		self.walkframe += 1
		imgframe = self.walkframe/4%(len(self.images['WALK']))
		self.image = self.images['WALK'][imgframe]
		
		if self.FACING < 0:
			self.image = pygame.transform.flip(self.image, True, False)

	def update_run(self):
		#self.update_status('RUNNING')
		imgframe = 0
		self.walkframe += 1
		imgframe = self.walkframe/2%(len(self.images['WALK']))
		self.image = self.images['WALK'][imgframe]
		
		if self.FACING < 0:
			self.image = pygame.transform.flip(self.image, True, False)

	def update_jump(self):
		if self.JUMPING_imgframe == len(self.images['JUMP']) - 1 :
		    	self.JUMPING_imgframe = len(self.images['JUMP']) - 1
		else :
			self.JUMPING_imgframe = self.jumpframe%(len(self.images['JUMP']))

		self.jumpframe += 1
		self.image = self.images['JUMP'][self.JUMPING_imgframe]

		if self.FACING < 0:
			self.image = pygame.transform.flip(self.image, True, False)

	def update_status(self, status):
	    	# 'HIT' means 'being HIT'
	    	__status = ['STANDING', 'WALKING', 'JUMPING', 'ATTACKING', 'RUNNING', 'HIT']
		for s in __status:
		    	if s == status:
			    	setattr(self, s , True)
			    	#setattr(self, s + "_imgframe" , 0)

			else :
			    	setattr(self, s , False)
		if status != 'STANDING':
			self.STANDING_imgframe = 0
			self.standframe = 0


	def update_images(self, images, frame):
		# one loop and stop in the last frame
		length = len(images)
		__fc  = 3
		if frame >= length * __fc :
			# stop in the last frame
			imgframe = length - 1
		else:
			imgframe = (frame/__fc)%length

		self.image = images[imgframe]

		if self.FACING < 0:
			self.image = pygame.transform.flip(self.image, True, False)

		frame += 1
		if frame > (length+1) * __fc:
			frame = 0
			self.update_status('STANDING')
		return frame


	def move(self, dx, dy):
		self.rect.x += dx
		self.rect.y += dy
		self.BASEMENT[0] += dx
		self.BASEMENT[1] += dy

	
	def test_hitting(self, enemy_layer):
	    	if self.HITZONE == None :
		    	return False
		for x in xrange(-5, 6):		#vertical hitting range
			#self.layer   same as self.group.get_layer_of_sprite(self), same as basement[1] also 
		    	enemy_list = enemy_layer.get_sprites_from_layer(self.layer + x)
			for enemy in enemy_list :
			    	if enemy is self:
				    	continue
				elif enemy.BODY is None:
				    	# maybe this is a shadow , or somewhat do not hitable
				    	continue
				elif self.HITZONE.colliderect(enemy.BODY):
				    	if enemy.HIT : # already in HIT status (maybe later give some combo here)
					    	continue
					else:
						enemy.update_status('HIT')

			
	def healthBar(self):
		if self.HIT :
			#pygame.draw.rect(self.screen, (255, 255, 0, 250), (self.rect.centerx-30, self.rect.bottom-6, 2*self.maxLife, 6))
			#pygame.draw.rect(self.screen, (255, 0, 0, 250), (self.rect.centerx-30, self.rect.bottom -5, 2*self.life, 4))
			pygame.draw.rect(self.screen, (255, 255, 0, 250), (self.BODY.centerx-20, self.BODY.top, 2*self.maxLife, 6))
			pygame.draw.rect(self.screen, (255, 0, 0, 250), (self.BODY.centerx-20, self.BODY.top +1, 2*self.life, 4))



class Shadow(GameSprite):
    	def __init__(self, pos , group):
	    	GameSprite.__init__(self, pos , 'Arthur.gif', group)
		self.images['STAND'].append(load_sheet2(self.sheet, (0, 0), (129, 248, 35, 14), (35, 14)))
		self.image =self.images['STAND'][0] 
		self.rect = self.image.get_rect()
		self.rect.midbottom = pos
		#print self.group.sprites()
	def update(self):
	    	pass

#under construction
class Background(pygame.sprite.Sprite):
    	def __init__(self, pos, filename, group, layer, screensize):
		print "create a BackGround"
		pygame.sprite.Sprite.__init__(self)
		self.sheet_ = load_image(os.path.join('gfx',filename), transparent = 0)
		self.sheet = load_sheet2(self.sheet_, (0, 0), (26, 1587, 1026, 257), (1026, 257), transparent=0)
		temp, temp , self.W , self.H = self.sheet.get_rect()
		self.layer = layer
		self.group = group
		self.width , self.height = screensize
		self.BASEMENT = list(pos) #bottomcenter (camara box)
		self.group.add(self, layer = self.layer)

	def move(self, camera_x = 0, camera_y = 0):
	    	# camera_x - self.width/2 >= 0  NEEDed
	    	self.screen_left = (camera_x - self.width/2) % self.W
		if self.screen_left >= 0 :
		    	if self.screen_left <= self.W - self.width:
			    	self.image = load_sheet2(self.sheet, (0, 0), (self.screen_left, 0, self.width, self.height), (self.width, self.height))
			elif self.screen_left > self.W - self.width and self.screen_left < self.W:
			    	self.image = load_sheet2(self.sheet, (0, 0), (self.screen_left, 0, self.width, self.height), (self.width, self.height)) 
				self.image.blit(self.sheet, (self.W - self.screen_left - 2, 0), (0, 0, self.width + self.screen_left - self.W, self.height)) 
			else:
			    	raise SystemExit, "camera wrong"
		self.rect = self.image.get_rect()


	def update(self):
	    	pass

class Scene():
	def __init__(self, filename, screensize):
	#def __init__(self, filename):
		self.sheet_ = load_image(os.path.join('gfx',filename), transparent = 0)
		self.image = load_sheet2(self.sheet_, (0, 0), (26, 1587, 1026, 257), (1026, 257), transparent=0)
		#self.image = pygame.image.load(filename).convert()
		temp, temp , self.W , self.H = self.image.get_rect()
		self.width , self.height = screensize

		self.x = 0

	def render(self, screen):
	    	if -self.x <= self.W - self.width:
			screen.blit(self.image, (self.x, 0))
		elif -self.x < self.W:
		    	screen.blit(self.image, (self.x, 0))
		    	screen.blit(self.image, (self.W + self.x - 2, 0))
		elif -self.x >= self.W:
		    	self.x = 0
			screen.blit(self.image, (self.x, 0))


	"""
	def render_loop(self, screen, speed):
	    	self.x += speed
		if sefl.x > self.W - self.width:
			screen.blit(self.image, (self.x, 0))
			screen.blit(self.image, (
		screen.blit(self.image, (self.x, 0))
	"""

class Arthur(GameSprite):
	def __init__(self, pos, group, scene = None, screen = None):
		GameSprite.__init__(self, pos , 'Arthur.gif', group, screen)
		self.images['NORMAL_SLASH'] = []
		self.images['JUMP_SLASH'] = []
		self.images['HEAVY_SLASH'] = []
		self.images['HEAVY_HIT'] = []
		# create stand images:
		#the FIRST 4 frame for STANDING , just run ONCE while arthur in standing pose
		#also finish his other action moving
		self.images['STAND'].append(load_sheet2(self.sheet, (0, 0), (354, 257, 70, 80), (70, 80)))
		self.images['STAND'].append(load_sheet2(self.sheet, (0, 0), (292, 233, 64, 106), (64, 106)))
		self.images['STAND'].append(load_sheet2(self.sheet, (0, 0), (292, 233, 64, 106), (64, 106)))
		self.images['STAND'].append(load_sheet2(self.sheet, (0, 0), (354, 257, 70, 80), (70, 80)))

		self.images['STAND'].append(load_sheet2(self.sheet, (0, 0), (0, 150, 70, 80), (70, 80)))
		self.images['STAND'].append(load_sheet2(self.sheet, (0, 0), (223, 72, 70, 80), (70, 80)))
		
		# create walk images:
		#self.images['WALK'].append()
		self.images['WALK'].append(load_sheet2(self.sheet, (-5, 0), (70, 150, 72, 80), (80, 80)))
		#self.images['WALK'].append(load_sheet2(self.sheet, (0, 0), (140, 150, 80, 80), (80, 80)))
		self.images['WALK'].append(load_sheet2(self.sheet, (0, 0), (142, 150, 80, 80), (80, 80)))
		self.images['WALK'].append(load_sheet2(self.sheet, (5, 0), (220, 150, 80, 80), (80, 80)))
		self.images['WALK'].append(load_sheet2(self.sheet, (0, 0), (300, 150, 80, 80), (80, 80)))
		self.images['WALK'].append(load_sheet2(self.sheet, (0, 0), (380, 150, 80, 80), (80, 80)))
		self.images['WALK'].append(load_sheet2(self.sheet, (0, 0), (460, 150, 80, 80), (80, 80)))

		# create jump images:
		self.images['JUMP'].append(load_sheet2(self.sheet, (0, 0), (292, 233, 64, 106), (64, 106)))
		self.images['JUMP'].append(load_sheet2(self.sheet, (0, 0), (61, 229, 64, 106), (64, 106)))
		#self.images['JUMP'].append(load_sheet2(self.sheet, (0, 0), (128, 266, 91, 65), (91, 65)))
		self.images['JUMP'].append(load_sheet2(self.sheet, (0, 0), (128, 266, 91, 65), (141, 65)))
	
		# create attack images:
		self.images['NORMAL_SLASH'].append(load_sheet2(self.sheet, (0, 0), (292, 233, 64, 106), (64, 106)))
		self.images['NORMAL_SLASH'].append(load_sheet2(self.sheet, (0, 0), (63, 361, 68, 83), (68, 83)))
		self.images['NORMAL_SLASH'].append(load_sheet2(self.sheet, (10, 0), (134, 365, 108, 79), (118, 79)))

		#create jump slash images:
		self.images['JUMP_SLASH'].append(load_sheet2(self.sheet, (0, 0), (128, 266, 91, 65), (141, 65)))
		self.images['JUMP_SLASH'].append(load_sheet2(self.sheet, (0, 0), (100, 451, 91, 65), (141, 65)))
		self.images['JUMP_SLASH'].append(load_sheet2(self.sheet, (0, 0), (195, 451, 91, 65), (91, 65)))
		self.images['JUMP_SLASH'].append(load_sheet2(self.sheet, (0, 0), (285, 451, 91, 65), (91, 65)))

		# create HEAVY SLASH images:
		self.images['HEAVY_SLASH'].append(load_sheet2(self.sheet, (0, 0), (128, 266, 91, 65), (141, 65)))

		# create being HIT images:
		self.images['HIT'].append(load_sheet2(self.sheet, (0, 0), (0, 888, 76, 61), (76, 61)))
		self.images['HIT'].append(load_sheet2(self.sheet, (0, 0), (81, 867, 64, 84), (64, 84)))

		self.image =self.images['STAND'][0] 
		self.rect = self.image.get_rect()
		self.rect.midbottom = pos

		self.BODY = copy(self.rect)	# protect your body ! my hero . (test this rect while being HIT)
		self.BODY.width = self.rect.width - 20		#make it a little bit smaller

		self.sound_slash = load_sound('slash.wav')
		self.speed = 0.1
		self.previous_key_time = 0
		self.combo = None
		self.controllable = True

		self.frame = 0   #common frame counter

		self.scene = scene
		self.feet = 0
	
	def update_stand(self):
	    	#some hardcode for this character . not good...
		if self.STANDING_imgframe <= 3:
			self.STANDING_imgframe = self.standframe/10%5
			
		if self.STANDING_imgframe >= 4:	
			self.STANDING_imgframe = self.standframe/15%2
			if self.STANDING_imgframe == 0:
			    	self.STANDING_imgframe = 4
			else:
			    	self.STANDING_imgframe = 5
			
		self.image = self.images['STAND'][self.STANDING_imgframe]
		
		if self.FACING < 0:
			self.image = pygame.transform.flip(self.image, True, False)
		self.standframe += 1

	def update_slash(self):
		self.SLASH_imgframe = self.slashframe/3%3
		self.image = self.images['NORMAL_SLASH'][self.SLASH_imgframe]
		
		if self.FACING < 0:
			self.image = pygame.transform.flip(self.image, True, False)
		self.slashframe += 1

		if self.slashframe > 9 :
			self.update_status('STANDING')
			self.slashframe = 0

	def update_jumpslash(self):
	    	#make these in special functions like 'update_ACTION()' , later ...
		#specify in : loops , one loop , one loop and state in the last frame etc.
		length = len(self.images['JUMP_SLASH'])
		if self.SLASH_imgframe == length - 1:
		    	self.SLASH_imgframe = length -1
			#slash is finish , but our hero is still in the sky ;)
			self.test_hitting(self.group)
		else:
			self.SLASH_imgframe = self.slashframe/3%length
		self.image = self.images['JUMP_SLASH'][self.SLASH_imgframe]

		if self.FACING < 0:
			self.image = pygame.transform.flip(self.image, True, False)

		self.slashframe += 1

	def update(self):
	    	GameSprite.update(self)
		if self.STANDING:
	    		self.update_stand()
		if self.WALKING:
	    		self.update_walk()
		if self.RUNNING:
	    		self.update_run()
		if self.JUMPING:
			self.jump(self.JUMPING_MOVE_SPEED)
			if self.JUMP_SLASH:
				self.update_jumpslash()
				#create the HITZONE
				self.HIT_ZONE()
			elif self.JUMPING:	
				self.update_jump()
		if self.ATTACKING:
			#self.update_slash()
		    	self.frame = self.update_images(self.images['NORMAL_SLASH'], self.frame)
			#create the HITZONE
			self.HIT_ZONE()

			#well . hard code is not good . fix it later
			if self.frame >= 9:
			    	self.test_hitting(self.group)

		if self.HIT:
		    	self.frame = self.update_images(self.images['HIT'], self.frame)
			if self.life > 0:
				self.life -= .4
			self.healthBar()

		#update layer
		self.layer = self.BASEMENT[1]
		self.group.change_layer(self, self.BASEMENT[1])
		
		self.rect = self.image.get_rect()

		if self.JUMPING == False:
			self.rect.midbottom = self.BASEMENT
		else : #jumping , we should do some special codes here
			self.rect.midbottom = self.jump_basement
		self.BODY.midbottom = self.rect.midbottom

	def Controllable(self):
	    	# CRICK ! or not 
		if (not self.ATTACKING) and (not self.JUMPING) and (not self.HIT):
		    	return True
		else:
		    	return False

	def ContralPanel(self):
	    	if button.is_pressed(RIGHT):
			if time.time() - self.previous_key_time > .25:
			    	self.combo = None
			if self.combo == None:
				self.previous_key_time = time.time()
				self.combo = "rr"
			elif self.combo == "rr":
			    	self.RUNNING = True
		if button.is_pressed(LEFT):
			if time.time() - self.previous_key_time > .25:
			    	self.combo = None
			if self.combo == None:
				self.previous_key_time = time.time()
				self.combo = "rl"
			elif self.combo == "rl":
			    	self.RUNNING = True
		if button.is_pressed(UP):
			if time.time() - self.previous_key_time > .25:
			    	self.combo = None
			if self.combo == None:
				self.previous_key_time = time.time()
				self.combo = "ru"
			elif self.combo == "ru":
			    	self.RUNNING = True
		if button.is_pressed(DOWN):
			if time.time() - self.previous_key_time > .25:
			    	self.combo = None
			if self.combo == None:
				self.previous_key_time = time.time()
				self.combo = "rd"
			elif self.combo == "rd":
			    	self.RUNNING = True

		if button.is_held(LEFT):
			if self.Controllable():
				self.normal_move(0)
			if (self.JUMPING) and (not self.JUMPING_MOVE):
			    	self.JUMPING_MOVE_SPEED = -2
				self.FACING = -1
				self.JUMPING_MOVE = True
			if self.JUMPING_MOVE :
			    	self.FACING = -1
	
		if button.is_held(RIGHT):
			if self.Controllable():
			    self.normal_move(2)
			if (self.JUMPING) and (not self.JUMPING_MOVE):
			    	self.JUMPING_MOVE_SPEED = 2
				self.FACING = 1
				self.JUMPING_MOVE = True
			if self.JUMPING_MOVE :
			    	self.FACING = 1

		if button.is_held(UP):
			if self.Controllable():
			    self.normal_move(3)

		if button.is_held(DOWN):
			if self.Controllable():
			    self.normal_move(1)
		
		if (not button.is_held(DOWN)) and (not button.is_held(UP)) and (not button.is_held(LEFT)) and (not button.is_held(RIGHT)):
		    	if self.Controllable():
				self.update_status('STANDING')

		if button.is_pressed(W):
		    	#now what ? just JUMP it !
			if (not self.ATTACKING) and (not self.JUMPING):
				self.slashframe = 0  # init slash pose
				self.jump_speed = -10
				self.JUMPING_imgframe = 0
				self.jump_basement = copy(self.BASEMENT)
			    	self.normal_jump()

		if button.is_pressed(D):
			if (not self.ATTACKING) and (not self.JUMPING) :
		    	# normal slash
				self.sound_slash.play()
			    	self.update_status('ATTACKING')
				self.SLASH_imgframe = 0
				# HIT HIT HIT !
				#self.test_hitting(self.group)

			elif (not self.ATTACKING) and (self.JUMPING) and (not self.JUMP_SLASH):
			# jumping slash
				self.sound_slash.play()
				self.SLASH_imgframe = 0
				self.JUMP_SLASH = True

			
		#test HIT :
		if button.is_pressed(S):
		    	self.update_status('HIT')
			self.frame  = 0

		"""
		
		if button.is_pressed(W):
		    	if not self.fatman.WALKING:
			    self.fatman.ATTACKING = True
			    self.fatman.update_attack()

		if button.is_pressed(D):
		    	aura = shield_aura(self.fatman.rect.center)
			self.bad_fat.update_hit()
		"""

	def normal_jump(self):
	    	self.update_status('JUMPING')
		#create a shadow for him
		#Shadow.group = self.group
		self.shadow = Shadow(self.BASEMENT, self.group)

	def jump(self, dx = 0):
		#self.jump_basement[1] += self.jump_speed
		#self.jump_basement[0] += dx
		#self.BASEMENT[0] += dx
		#self.jump_speed += 1
		#self.shadow.rect.midbottom = self.BASEMENT

		#update the scene where this sprite belong to be
		#if self.scene is not None:
		#	self.scene.move(-dx/2, 0)
		self.camera_move(dx, 0)
		self.feet += dx

		if self.jump_basement[1] >= self.BASEMENT[1]:
		    	#when he landed , init all things 
			self.jump_basement[1] = self.BASEMENT[1]
			self.jump_speed = 0
			self.JUMPING_MOVE_SPEED = 0
			self.JUMPING = False
			self.JUMPING_MOVE = False

			self.SLASH_imgframe = 0
			self.slashframe = 0
			self.JUMP_SLASH = False

			self.update_status('STANDING')
			self.shadow.kill()

	def normal_move(self, direction):
	    	# this is for bad guy usage , but later  . bad guy is in another class !
		# any how just for test
		if not self.Controllable():
			return

		if self.RUNNING:
			self.update_status('RUNNING')
			speed = 5
		else:
			self.update_status('WALKING')
			speed = 2 

		if direction == 0:
			self.FACING = -1
		elif direction == 2:
			self.FACING = 1

		if direction == 0:
			#self.move(-speed, 0)
			self.feet -= speed
			self.camera_move(-speed, 0)
		if direction == 2:
			#self.move(speed, 0)
			self.feet += speed
			self.camera_move(speed, 0)
		if direction == 3:
			self.move(0, -speed)
		if direction == 1:
			self.move(0, speed)

	def camera_move(self, dx, dy):
	    	#now x-axis ONLY 
		if self.scene is not None:
			center = self.scene.width / 2
		else:
		    	# not good
		    	center = 0
		if not self.JUMPING:
			if (self.scene is not None) and ((self.BODY.midbottom[0] > center + 100) and self.FACING == 1) or ((self.BODY.midbottom[0] < center - 100) and self.FACING == -1):
				#move the camera 
				#main character stay still , the world need to move around him
				self.scene.move(-dx, 0 )

			else:
				#camera stay still
				#all things stay still , except main character is moving 
				self.move(dx ,0)	
		elif self.JUMPING:
			self.jump_basement[1] += self.jump_speed
			self.jump_speed += 1

			if (self.scene is not None) and ((self.BODY.midbottom[0] > center + 100) and dx > 0) or ((self.BODY.midbottom[0] < center - 100) and dx<0):
				#move the camera 
				#main character stay still , the world need to move around him
				self.scene.move(-dx, 0 )
			else:
				#camera stay still
				#all things stay still , except main character is moving 
				self.jump_basement[0] += dx
				self.BASEMENT[0] += dx

			self.shadow.rect.midbottom = self.BASEMENT
	
	def HIT_ZONE(self):
	    	self.HITZONE = pygame.Rect(0,0, self.rect.width/2, self.rect.height) 
		if self.FACING == -1:
			self.HITZONE.bottomright = self.rect.midbottom
		if self.FACING == 1:
			self.HITZONE.bottomleft = self.rect.midbottom


	    	
class Scene_Layer():
	def __init__(self, filename, screensize, area, size, pos=(0,0), group=None):
		self.sheet_ = load_image(os.path.join('gfx',filename), transparent = 0)
		self.image = load_sheet2(self.sheet_, (0, 0), area, size, transparent=0)
		temp, temp , self.W , self.H = self.image.get_rect()
		self.width , self.height = screensize

		self.x, self.y = pos
		self.dx = self.dy = 0
		self.group = group
		print self.group, "hihi"

	def move(self, dx, dy):
		self.x += dx
		self.y += dy
		self.dx = dx
		self.dy = dy

	def render(self, screen):
	    	#for now , this function is support x-axis scrolling only 

	    	if -self.x <= self.W - self.width:
		    	if self.x <=0:
				screen.blit(self.image, (self.x, self.y))
			else:
				screen.blit(self.image, (self.x, self.y))
				screen.blit(self.image, (self.x - self.W, self.y))

		elif -self.x < self.W:
		    	screen.blit(self.image, (self.x, self.y))
		    	screen.blit(self.image, (self.W + self.x - 2, self.y))
		if abs(self.x) >= self.W:
		    	self.x = 0
			screen.blit(self.image, (self.x, self.y))

		if self.group is not None:
			for __sprite in self.group:
			    	__sprite.move(self.dx, self.dy)
			self.dx = self.dy = 0


	"""
	def render_loop(self, screen, speed):
	    	self.x += speed
		if sefl.x > self.W - self.width:
			screen.blit(self.image, (self.x, 0))
			screen.blit(self.image, (
		screen.blit(self.image, (self.x, 0))
	"""
